﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Geekees.Common.Controls;
using EasyBuildingCMS.BLL.CoreEngine;

namespace EasyBuildingCMS.BuildingBlocks.Core.Navigation.TreeView
{
    public partial class TreeViewRenameNodeHandle : System.Web.UI.Page
    {
        ASTreeViewAjaxReturnCode returnCode;
        string errorMessage = string.Empty;

        public string NodeValue
        {
            get
            {
                return HttpUtility.UrlDecode(Request.QueryString["nodeValue"]);
            }
        }

        public string NewNodeText
        {
            get
            {
                return HttpUtility.UrlDecode(Request.QueryString["newNodeText"]);
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.NodeValue) || string.IsNullOrEmpty(this.NewNodeText))
            {
                returnCode = ASTreeViewAjaxReturnCode.ERROR;
                return;
            }

            try
            {
                this.RenameNode();
            }
            catch (Exception ex)
            {
                this.returnCode = ASTreeViewAjaxReturnCode.ERROR;
                this.errorMessage = ex.Message;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.returnCode == ASTreeViewAjaxReturnCode.OK)
                writer.Write((int)this.returnCode);
            else
                writer.Write(this.errorMessage);
        }

        protected void RenameNode()
        {
            PageRepository pageRepository = new PageRepository();
            pageRepository.UpdatePageNavigationText(Int32.Parse(this.NodeValue), this.NewNodeText);
        }
    }
}